"""
通过本模块打开二进制文件，并转为YCrCb空间，用list记录
"""

import struct
import os

# 导入文件操作相关的packages


binfile = open('demo.bin', 'rb')  # 打开文件
size = os.path.getsize('demo.bin')  # 记录文件大小
a = []
for i in range(size):
    data = binfile.read(1)
    a.append(struct.unpack('B', data)[0])
    # 一个一个字节，读取二进制文件
binfile.close()
# 颜色空间变换
Y = []
Cr = []
Cb = []

for x in range(0, len(a)):
    if x % 3 == 0:
        R = a[x]
    if x % 3 == 1:
        G = a[x]
    if x % 3 == 2:
        B = a[x]
        Y.append(0.299 * R + 0.587 * G + 0.114 * B)
        Cr.append(0.5 * R - 0.4187 * G - 0.0813 * B + 128)
        Cb.append(-0.1687 * R - 0.3313 * G + 0.5 * B + 128)
